/*
 * dri_hal_fatfs.c
 *
 *  Created on: 2023年2月27日
 *      Author: Lw
 */
#include "dri_hal_fatfs.h"
#include "update_firmware.h"
#include "cali_panel.h"
#include "fpga_reg.h"

static FIL fil;     /* File object */
static FATFS fatfs;
char work[FF_MAX_SS];


FRESULT emmcInit(void)
{
    FRESULT rc;
    TCHAR *EmmcPath0 = "0:/";
//  TCHAR *EmmcPath1 = "1:/";
//  TCHAR *EmmcPath2 = "2:/";
//    UINT bw;
//    u8 initData[RECV_PACKAGE_PACKAGE_MAX_NUM] = {0};
    /* 挂载emmc */
    rc = f_mount(&fatfs, EmmcPath0, 1);
    if (rc != FR_OK)
    {
        rc = f_mkfs(EmmcPath0, FM_FAT32, 0, work, sizeof(work));
        if (rc != FR_OK){
            return rc;
        }
    }
    /* 打开一个文件，如果不存在则创建  */
    rc = f_open(&fil, EMMC_ENDPackageSeq_FILE_NAME, FA_OPEN_EXISTING);
    if(rc != FR_OK)
    {
        rc = f_open(&fil, EMMC_ENDPackageSeq_FILE_NAME, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
        	return rc;
        }
    }
    f_close(&fil);
    /* 打开一个文件，如果不存在则创建  */
    rc = f_open(&fil, EMMC_TotalPackageNum_FILE_NAME, FA_OPEN_EXISTING);
    if(rc != FR_OK)
    {
        rc = f_open(&fil, EMMC_TotalPackageNum_FILE_NAME, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
    }
    f_close(&fil);
    /* 打开一个文件，如果不存在则创建  */
    rc = f_open(&fil, EMMC_TotalCompressDataLen_FILE_NAME, FA_OPEN_EXISTING);
    if(rc != FR_OK)
    {
        rc = f_open(&fil, EMMC_TotalCompressDataLen_FILE_NAME, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
        	return rc;
        }
    }
    f_close(&fil);
    /* 打开一个文件，如果不存在则创建  */
    rc = f_open(&fil, EMMC_TotalPackage_STATUS, FA_OPEN_EXISTING);
    if(rc != FR_OK)
    {
        rc = f_open(&fil, EMMC_TotalPackage_STATUS, FA_CREATE_ALWAYS | FA_WRITE);
        if(rc != FR_OK){
            return rc;
        }
    }
    f_close(&fil);

    return rc;
}

/* emmc读数据  */
 FRESULT emmcReadData(char *file_name, u8 *buff, u32 data_len)
{
    /* f_read 返回的已读取的字节数  */
    UINT br;
    FRESULT res;

    /* 打开一个只读的文件  */
    res = f_open(&fil, file_name, FA_READ);
    if(res != FR_OK)
    {
        return res;
    }

    /* 从emmc中读出数据  */
    res = f_read(&fil, (void *)buff, data_len, &br);
    if(res != FR_OK)
    {
        f_close(&fil);
        return res;
    }

    /* 关闭文件  */
    f_close(&fil);

    return res;
}

/* emmc写数据
 *
 * addr :写入的文件地址，文件开头地址为0，文件的末尾地址为当前文件的字节大小。
 *
 * */
 FRESULT emmcWriteData(char *file_name, u8 *buff, u32 byte_len, u32 addr)
{
    /* f_write 返回的已写入的字节数  */
    UINT bw;
    FRESULT res;

    /* 打开一个文件，如果不存在则创建  */
    res = f_open(&fil, file_name, FA_OPEN_EXISTING | FA_WRITE);
    if(res != FR_OK)
    {
        res = f_open(&fil, file_name, FA_CREATE_ALWAYS | FA_WRITE);
        if(res != FR_OK)
        {
            return res;
        }
    }
    /*移动文件指针*/
    f_lseek(&fil, addr);

    /* 向emmc中写入数据  */
    res = f_write(&fil, (void *)buff, byte_len, &bw);
    if(res != FR_OK)
    {
        f_close(&fil);
        return res;
    }

    /* 关闭文件  */
    f_close(&fil);

    return res;
}

/* emmc写参数数据
 */
 FRESULT emmcWriteParaData(char *file_name, u32 para_data, u32 byte_len)
{
    FRESULT res;
    u8 tempData[4]={0};

    tempData[0] = (u8)para_data;
    tempData[1] = (u8)(para_data >> 8);
    tempData[2] = (u8)(para_data >> 16);
    tempData[3] = (u8)(para_data >> 24);
    res = emmcWriteData(file_name, tempData, byte_len, 0);
    return res;
}
/* emmc读参数数据  */
 u32 emmcReadParaData(char *file_name,u32 data_len)
{
    u32 data_para = 0;
    u8 tempData[4]={0};
    emmcReadData(file_name,tempData,data_len);
    data_para = (tempData[3]<<24) + (tempData[2]<<16) + (tempData[1]<<8) + (tempData[0]);
    return data_para;
}
/* Compare memory block */
 int mem_cmp (const void* dst, const void* src, UINT cnt) /* ZR:same, NZ:different */
{
    const BYTE *d = (const BYTE *)dst, *s = (const BYTE *)src;
    int r = 0;

    do {
        r = *d++ - *s++;
    } while (--cnt && r == 0);

    return r;
}




